package com.tj.collection.controller.lending.response;


import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.EntityUtils;
import com.tj.collection.db.entity.ClMerchantEntity;
import com.tj.collection.db.entity.business.ClOrderAllocationEntity;
import com.tj.collection.utils.CalculatorUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/9/10
 * Time: 下午5:15
 */
public class ClOrderQueryResponse implements ResponseData {

    private List<Item> itemList = new ArrayList<>();

    public ClOrderQueryResponse(List<ClOrderAllocationEntity> clOrderAllocationEntityList, List<ClMerchantEntity> clMerchantEntityList) {
        Map<String, ClMerchantEntity> clMerchantEntityMap = EntityUtils.makeEntityMap(clMerchantEntityList, "merchantCode");

        for (ClOrderAllocationEntity clOrderAllocationEntity : clOrderAllocationEntityList) {

            BigDecimal expireAmount = new BigDecimal(0);//应还总金额
            BigDecimal noRepayAmount = new BigDecimal(0);//待还款金额
            BigDecimal lateFee = new BigDecimal(0);//总罚息
            BigDecimal overdueServiceFee = BigDecimal.ZERO;
            BigDecimal overdueFee = BigDecimal.ZERO;
            if (clOrderAllocationEntity.getOverdueDays() > 0) {
                int overdueDays = CalculatorUtils.overDueDays(clOrderAllocationEntity.getRepaymentDate(), new Date());
                overdueServiceFee = clOrderAllocationEntity.getLateServiceFee();
                overdueFee = CalculatorUtils.calculateOverdueFee(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getLateFeeRatio(), overdueDays);
            }
            lateFee = overdueFee.add(overdueServiceFee);

            expireAmount = CalculatorUtils.repaymentTotalCapital(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getInterest(), overdueFee, overdueServiceFee);
            noRepayAmount = expireAmount.subtract(clOrderAllocationEntity.getPaidAmount()).subtract(clOrderAllocationEntity.getDeductAmount());

            if (noRepayAmount.compareTo(BigDecimal.ZERO) < 0) {
                noRepayAmount = BigDecimal.ZERO;
            }

            expireAmount = expireAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
            noRepayAmount = noRepayAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
            lateFee = lateFee.setScale(2, BigDecimal.ROUND_HALF_UP);

            Item item = new Item()
                    .setName(clOrderAllocationEntity.getName())
                    .setMobile(clOrderAllocationEntity.getMobile())
                    .setIdentityNo(clOrderAllocationEntity.getIdentityNo())
                    .setRealCapital(clOrderAllocationEntity.getBorrowCapital())
                    .setExpireAmount(expireAmount)
                    .setPaidAmount(clOrderAllocationEntity.getPaidAmount())
                    .setLateFee(lateFee)
                    .setFollowUp(clOrderAllocationEntity.getAgentName())
                    .setUserCode(clOrderAllocationEntity.getUserCode())
                    .setCollectionNo(clOrderAllocationEntity.getCollectionNo())
                    .setTradeNo(clOrderAllocationEntity.getOrderNo())
                    .setStageCode(clOrderAllocationEntity.getStageCode())
                    .setUserLanguage(clOrderAllocationEntity.getUserLanguage())
                    .setSourceMerchantCode(clOrderAllocationEntity.getSourceMerchantCode())
                    .setProductCategory(clOrderAllocationEntity.getProductType())
                    .setProductCategoryName(clOrderAllocationEntity.getProductType());
//                    .setProductCategoryName(clMerchantEntityMap.containsKey(clOrderAllocationEntity.getSourceMerchantCode()) ? clMerchantEntityMap.get(clOrderAllocationEntity.getSourceMerchantCode()).getAppName() : clOrderAllocationEntity.getSourceMerchantCode());
            //账单描述信息
            List<BillItem> billItems = new ArrayList<>();
            BillItem billItem = new BillItem()
                    .setTradeNo(clOrderAllocationEntity.getOrderNo())
                    .setPeriod(clOrderAllocationEntity.getPeriod())
                    .setRepaymentDate(clOrderAllocationEntity.getRepaymentDate())
                    .setRealCapital(clOrderAllocationEntity.getBorrowCapital())
                    .setInterest(clOrderAllocationEntity.getInterest())
                    .setLateFee(lateFee)
                    .setNoRepayAmount(noRepayAmount)
                    .setExpireAmount(expireAmount)
                    .setOverdueDays(clOrderAllocationEntity.getOverdueDays())
                    .setActualRepaymentDate(clOrderAllocationEntity.getActualRepaymentDate())
                    .setPaidAmount(clOrderAllocationEntity.getPaidAmount())
                    .setDeductAmount(clOrderAllocationEntity.getDeductAmount());
            billItems.add(billItem);
            item.setCollectionOrderDetailVoList(billItems);
            this.itemList.add(item);
        }
    }

    private class Item {

        private String userCode;

        private String stageCode;

        private String collectionNo;

        private String tradeNo;

        private String name;

        private String mobile;

        private String identityNo;

        private BigDecimal realCapital;

        private BigDecimal expireAmount;

        private BigDecimal paidAmount;

        private BigDecimal lateFee;

        private String followUp;

        /**
         * 语言
         */
        private String userLanguage;
        /**
         * 商户标识
         */
        private String sourceMerchantCode;

        private String productCategory;

        private String productCategoryName;

        private List<BillItem> collectionOrderDetailVoList;

        public String getUserCode() {
            return userCode;
        }

        public Item setUserCode(String userCode) {
            this.userCode = userCode;
            return this;
        }

        public String getStageCode() {
            return stageCode;
        }

        public Item setStageCode(String stageCode) {
            this.stageCode = stageCode;
            return this;
        }

        public String getCollectionNo() {
            return collectionNo;
        }

        public Item setCollectionNo(String collectionNo) {
            this.collectionNo = collectionNo;
            return this;
        }

        public String getTradeNo() {
            return tradeNo;
        }

        public Item setTradeNo(String tradeNo) {
            this.tradeNo = tradeNo;
            return this;
        }

        public String getName() {
            return name;
        }

        public Item setName(String name) {
            this.name = name;
            return this;
        }

        public String getMobile() {
            return mobile;
        }

        public Item setMobile(String mobile) {
            this.mobile = mobile;
            return this;
        }

        public String getIdentityNo() {
            return identityNo;
        }

        public Item setIdentityNo(String identityNo) {
            this.identityNo = identityNo;
            return this;
        }

        public BigDecimal getRealCapital() {
            return realCapital;
        }

        public Item setRealCapital(BigDecimal realCapital) {
            this.realCapital = realCapital;
            return this;
        }

        public BigDecimal getExpireAmount() {
            return expireAmount;
        }

        public Item setExpireAmount(BigDecimal expireAmount) {
            this.expireAmount = expireAmount;
            return this;
        }

        public BigDecimal getPaidAmount() {
            return paidAmount;
        }

        public Item setPaidAmount(BigDecimal paidAmount) {
            this.paidAmount = paidAmount;
            return this;
        }

        public BigDecimal getLateFee() {
            return lateFee;
        }

        public Item setLateFee(BigDecimal lateFee) {
            this.lateFee = lateFee;
            return this;
        }

        public String getFollowUp() {
            return followUp;
        }

        public Item setFollowUp(String followUp) {
            this.followUp = followUp;
            return this;
        }

        public String getUserLanguage() {
            return userLanguage;
        }

        public Item setUserLanguage(String userLanguage) {
            this.userLanguage = userLanguage;
            return this;
        }

        public String getSourceMerchantCode() {
            return sourceMerchantCode;
        }

        public Item setSourceMerchantCode(String sourceMerchantCode) {
            this.sourceMerchantCode = sourceMerchantCode;
            return this;
        }

        public String getProductCategory() {
            return productCategory;
        }

        public Item setProductCategory(String productCategory) {
            this.productCategory = productCategory;
            return this;
        }

        public String getProductCategoryName() {
            return productCategoryName;
        }

        public Item setProductCategoryName(String productCategoryName) {
            this.productCategoryName = productCategoryName;
            return this;
        }

        public List<BillItem> getCollectionOrderDetailVoList() {
            return collectionOrderDetailVoList;
        }

        public Item setCollectionOrderDetailVoList(List<BillItem> collectionOrderDetailVoList) {
            this.collectionOrderDetailVoList = collectionOrderDetailVoList;
            return this;
        }
    }

    private static class BillItem {
        private String tradeNo;
        private Integer period;
        private Date repaymentDate;
        private BigDecimal realCapital;
        private BigDecimal interest;
        private BigDecimal lateFee;
        private BigDecimal noRepayAmount;
        private BigDecimal expireAmount;
        private Integer overdueDays;
        private Date actualRepaymentDate;
        private BigDecimal deductAmount;
        private BigDecimal paidAmount;

        public String getTradeNo() {
            return tradeNo;
        }

        public BillItem setTradeNo(String tradeNo) {
            this.tradeNo = tradeNo;
            return this;
        }

        public Integer getPeriod() {
            return period;
        }

        public BillItem setPeriod(Integer period) {
            this.period = period;
            return this;
        }

        public Date getRepaymentDate() {
            return repaymentDate;
        }

        public BillItem setRepaymentDate(Date repaymentDate) {
            this.repaymentDate = repaymentDate;
            return this;
        }

        public BigDecimal getRealCapital() {
            return realCapital;
        }

        public BillItem setRealCapital(BigDecimal realCapital) {
            this.realCapital = realCapital;
            return this;
        }

        public BigDecimal getInterest() {
            return interest;
        }

        public BillItem setInterest(BigDecimal interest) {
            this.interest = interest;
            return this;
        }

        public BigDecimal getLateFee() {
            return lateFee;
        }

        public BillItem setLateFee(BigDecimal lateFee) {
            this.lateFee = lateFee;
            return this;
        }

        public BigDecimal getNoRepayAmount() {
            return noRepayAmount;
        }

        public BillItem setNoRepayAmount(BigDecimal noRepayAmount) {
            this.noRepayAmount = noRepayAmount;
            return this;
        }

        public BigDecimal getExpireAmount() {
            return expireAmount;
        }

        public BillItem setExpireAmount(BigDecimal expireAmount) {
            this.expireAmount = expireAmount;
            return this;
        }

        public Integer getOverdueDays() {
            return overdueDays;
        }

        public BillItem setOverdueDays(Integer overdueDays) {
            this.overdueDays = overdueDays;
            return this;
        }

        public Date getActualRepaymentDate() {
            return actualRepaymentDate;
        }

        public BillItem setActualRepaymentDate(Date actualRepaymentDate) {
            this.actualRepaymentDate = actualRepaymentDate;
            return this;
        }

        public BigDecimal getDeductAmount() {
            return deductAmount;
        }

        public BillItem setDeductAmount(BigDecimal deductAmount) {
            this.deductAmount = deductAmount;
            return this;
        }

        public BigDecimal getPaidAmount() {
            return paidAmount;
        }

        public BillItem setPaidAmount(BigDecimal paidAmount) {
            this.paidAmount = paidAmount;
            return this;
        }
    }
}
